package net.droiding.data;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import net.droiding.misc.DroidingException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DroidingParser {
	
	private static final String VERSION = "1.0";
	
	public static Provider[] parseProviders(byte[] xml) throws Exception {
		DocumentBuilder builder = null;
		Document doc = null;
		builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		doc = builder.parse(new ByteArrayInputStream(xml));
		
		checkDoc(doc);
		NodeList providers = doc.getElementsByTagName("provider");
		int noOfProviders = providers.getLength();
		Provider[] provs = new Provider[noOfProviders];
		for (int i = 0; i < noOfProviders; i++) {
			Element provider = (Element) providers.item(i);
			String key = provider.getAttribute("key");
			String name = provider.getAttribute("name");
			String location = provider.getAttribute("location");
			String latitude = provider.getAttribute("lat");
			String longitude = provider.getAttribute("lng");
			int zoom = Integer.parseInt(provider.getAttribute("zoom"));
			long lastupdate = Long.parseLong(provider.getAttribute("lastupdate"));
			provs[i] = new Provider(key, name, location, latitude, longitude, zoom, lastupdate);
		}
		return provs;

	}
	public static Station[] parseStations(byte[] xml) throws Exception {
		DocumentBuilder builder = null;
		Document doc = null;
		builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		doc = builder.parse(new ByteArrayInputStream(xml));
		 
		checkDoc(doc);
		NodeList el_stations = doc.getElementsByTagName("station");
		int noOfStations = el_stations.getLength();
		Station[] stations = new Station[noOfStations];
		for (int i = 0; i < noOfStations; i++) {
			Element el_station = (Element) el_stations.item(i);
			int remoteId = Integer.parseInt(el_station.getAttribute("remoteid"));
			String key = el_station.getAttribute("skey");
			String pkey = el_station.getAttribute("pkey");
			String location = el_station.getAttribute("name");
			String latitude = el_station.getAttribute("lat");
			String longitude = el_station.getAttribute("lng");
			int bikes = Integer.parseInt(el_station.getAttribute("bikes"));
			int slots = Integer.parseInt(el_station.getAttribute("slots"));
			long lastupdate = Long.parseLong(el_station.getAttribute("lastupdate"));
			stations[i] = new Station(remoteId, key, pkey, location, latitude, longitude, bikes, slots, lastupdate, false);
		}
		return stations;
	}
	public static String parseLogin(byte[] xml) throws Exception {
		DocumentBuilder builder = null;
		Document doc = null;
		builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		doc = builder.parse(new ByteArrayInputStream(xml));
		checkDoc(doc);
		NodeList loginList = doc.getElementsByTagName("login");
		if (loginList.getLength() != 1) {
			checkError(doc);
			return null;
		}
		Element login = (Element) loginList.item(0);
		return login.getAttribute("username");
	}
	private static void checkError(Document doc) throws Exception {
		NodeList errorList = doc.getElementsByTagName("error");
		if (errorList.getLength() != 1) {
			throw new Exception("Not recognized error !");
		}
		Element error = (Element) errorList.item(0);
		String type = error.getAttribute("type");
		String message = error.getAttribute("message");
		throw new DroidingException(type, message);
		
	}
	private static void checkDoc(Document doc) throws Exception {
		Element el = doc.getDocumentElement();
		if (!"droiding".equals(el.getNodeName()))
			throw new Exception("invalid document !");
		String version = el.getAttribute("version");
		if (!VERSION.equals(version))
			throw new Exception("invalid droiding document version !");
	}
//	public static List<Station> parseStations() {
//		
//	}

}
